package com.lp.task;

import com.lp.bo.TbRealMonData03BO;
import com.lp.service.IotHistorySensorDataService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.*;
import java.util.List;
import java.util.UUID;

@Component
public class DataSyncTask {

    @Resource
    public IotHistorySensorDataService iotHistorySensorDataService;

//    private static final String ORACLE_URL = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
//    private static final String ORACLE_USER = "SYSTEM";
//    private static final String ORACLE_PASSWORD = "manager";
    private static final String ORACLE_URL = "jdbc:oracle:thin:@10.1.21.254:12345:ORCL";
    private static final String ORACLE_USER = "YJGLJ_FXJC_1";
    private static final String ORACLE_PASSWORD = "baosight123";
//    private static final String ORACLE_URL = "jdbc:mysql://127.0.0.1:3306/easyiot?characterEncoding=utf8";
//    private static final String ORACLE_USER = "root";
//    private static final String ORACLE_PASSWORD = "root";

    @Scheduled(cron = "0 */5 * * * ?")
//    @Scheduled(fixedRate = 5000)
    // 从MySQL数据库获取数据并插入到Oracle数据库
    private void syncDataFromMySQLToOracle() throws ClassNotFoundException {
        Class.forName("oracle.jdbc.driver.OracleDriver");

        // 插入数据到Oracle
        String insertSQL = "INSERT INTO YJGLJ_FXJC.TB_REAL_MON_DATA03 (st_data_id, target_code, st_data, collect_time) VALUES (?, ?, ?, ?)"; // 修改为你的Oracle表结构

//        String insertSQL = "INSERT INTO TB_REAL_MON_DATA03 (st_data_id, target_code, st_data, collect_time) VALUES (?, ?, ?, ?)"; // 修改为你的Oracle表结构

        try (Connection oracleConnection = DriverManager.getConnection(ORACLE_URL, ORACLE_USER, ORACLE_PASSWORD);
             PreparedStatement oraclePreparedStatement = oracleConnection.prepareStatement(insertSQL)) {
            // 从MySQL结果集中获取数据
            List<TbRealMonData03BO> list = iotHistorySensorDataService.selectHistoryData();

            for (TbRealMonData03BO tbRealMonData03BO : list) {
                // 从MySQL结果集中获取数据
                String column1Data = UUID.randomUUID().toString().replace("-","");  // 修改为你的字段名称
                String column2Data = tbRealMonData03BO.getTarget_code();  // 修改为你的字段名称
                Double column3Data = tbRealMonData03BO.getSt_data();  // 修改为你的字段名称
                Timestamp column4Data = tbRealMonData03BO.getCollect_time();  // 修改为你的字段名称

                // 设置Oracle插入语句的参数
                oraclePreparedStatement.setString(1, column1Data);
                oraclePreparedStatement.setString(2, column2Data);
                oraclePreparedStatement.setDouble(3, column3Data);
                oraclePreparedStatement.setTimestamp(4, column4Data);

                // 执行插入
                oraclePreparedStatement.addBatch();
            }

            // 执行批量插入
            int[] affectedRecords = oraclePreparedStatement.executeBatch();
            System.out.println("实时数据上传成功！ Number of records affected: " + affectedRecords.length);
        } catch (SQLException e) {
            System.err.println("Database synchronization failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
